package com.zelin.manager.service.impl;

import com.zelin.common.utils.PageUtils;
import com.zelin.manager.dao.SpecificationOptionDao;
import com.zelin.manager.entity.SpecificationOptionEntity;
import com.zelin.manager.group.Specification;
import com.zelin.manager.service.SpecificationOptionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zelin.common.utils.Query;
import com.zelin.manager.dao.SpecificationDao;
import com.zelin.manager.entity.SpecificationEntity;
import com.zelin.manager.service.SpecificationService;


@Service("specificationService")
public class SpecificationServiceImpl extends ServiceImpl<SpecificationDao, SpecificationEntity> implements SpecificationService {

    @Autowired
    private SpecificationOptionService optionService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SpecificationEntity> page = this.page(
                new Query<SpecificationEntity>().getPage(params),
                new QueryWrapper<SpecificationEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 功能描述:添加规格
     * 返回值:
     * 作者:LZB
     * 时间: 2021/10/29 11:31
     */
    @Override
    public void insert(Specification specification) {
        //得到规格对象
        SpecificationEntity spec = specification.getSpec();
        //添加规格对象，此时有了主键值，（具体说：spec对象有了主键值）
        baseMapper.insert(spec);
        //得到规格选项
        for (SpecificationOptionEntity option : specification.getOptions()) {
            //綁定外鍵
            option.setSpecId(spec.getId());
            //添加規格選項
            optionService.save(option);
        }
    }

    /**
     * 功能描述:根據規格選項複合對象及id查詢規格
     * 返回值:
     * 作者:LZB
     * 时间: 2021/11/1 11:07
     */
    @Override
    public Specification findById(Long id) {
        //根據id查詢規格
        SpecificationEntity spec = baseMapper.selectById(id);
        //根據id查詢規格選項列表
        //封裝查詢條件
        QueryWrapper<SpecificationOptionEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("spec_id",id);
        //根據外鍵查詢得到規格選項列表
        List<SpecificationOptionEntity> options = optionService.list(wrapper);
        //定義組合對象
        Specification specification = new Specification();
        specification.setSpec(spec);
        specification.setOptions(options);
        return specification;
    }

    /**
     * 功能描述:修改規格及規格選項
     * 返回值:
     * 作者:LZB
     * 时间: 2021/11/1 11:35
     */
    @Override
    public void update(Specification specification) {
        //得到spec的值
        SpecificationEntity spec = specification.getSpec();
        //修改规格
        baseMapper.updateById(spec);
        //根据规格id在选项表中删除记录（tb_specificationOption_option）
        optionService.remove(new QueryWrapper<SpecificationOptionEntity>().eq("spec_id",spec.getId()));

        //添加规格数据到tb_specificationOption_option
        List<SpecificationOptionEntity> options = specification.getOptions();
        for (SpecificationOptionEntity option:options) {
            //绑定规格选项的id
            option.setSpecId(spec.getId());
            //添加数据到规格选项表中
            optionService.save(option);
        }


    }

    /**
     * 功能描述:根据id删除规格及规格选项
     * 返回值:
     * 作者:LZB
     * 时间: 2021/11/2 20:05
     */
    @Override
    public void delete(List<Long> ids) {
        //遍历ids得到id值
        for (Long id:ids) {
            //删除规格
            baseMapper.deleteById(id);
            //删除规格选项
            optionService.remove(new QueryWrapper<SpecificationOptionEntity>().eq("spec_id",id));
        }
    }

}